package com.cskaoyan.service;

import com.cskaoyan.bean.MarketSystem;
import com.cskaoyan.bean.MarketSystemExample;
import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.util.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

public class ConfigServiceImpl implements ConfigService{
    @Override
    public Map<String, String> mallQuery() {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketSystemMapper mapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        List<MarketSystem> systems = mapper.selectByExample(example);
        Map<String,String> map = new HashMap<>();
        for (MarketSystem system : systems) {
            String keyName = system.getKeyName();
            switch (keyName){
                case "market_mall_address":
                    map.put("market_mall_address",system.getKeyValue());
                    break;
                case "market_mall_latitude":
                    map.put("market_mall_latitude",system.getKeyValue());
                    break;
                case "market_mall_longitude":
                    map.put("market_mall_longitude",system.getKeyValue());
                    break;
                case "market_mall_name":
                    map.put("market_mall_name",system.getKeyValue());
                    break;
                case "market_mall_phone":
                    map.put("market_mall_phone",system.getKeyValue());
                    break;
                case "market_mall_qq":
                    map.put("market_mall_qq",system.getKeyValue());
                    break;
            }
        }
        sqlSession.close();
        return map;
    }

    @Override
    public void mallModify(Map<String, String> map) {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketSystemMapper mapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        List<MarketSystem> systems = mapper.selectByExample(example);
        //更新数据
        for (MarketSystem system : systems) {
            String keyName = system.getKeyName();
            switch (keyName){
                case "market_mall_address":
                    system.setKeyValue(map.get("market_mall_address"));
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_mall_latitude":
                    system.setKeyValue(map.get("market_mall_latitude"));
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_mall_longitude":
                    system.setKeyValue(map.get("market_mall_longitude"));
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_mall_name":
                    system.setKeyValue(map.get("market_mall_name"));
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_mall_phone":
                    system.setKeyValue(map.get("market_mall_phone"));
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_mall_qq":
                    system.setKeyValue(map.get("market_mall_qq"));
                    mapper.updateByPrimaryKey(system);
                    break;
            }

        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Map<String, Object> wxQuery() {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketSystemMapper mapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        List<MarketSystem> systems = mapper.selectByExample(example);
        Map<String,Object> map = new HashMap<>();
        for (MarketSystem system : systems) {
            String keyName = system.getKeyName();
            switch (keyName){
                case "market_wx_catlog_goods":
                    map.put("market_wx_catlog_goods",system.getKeyValue());
                    break;
                case "market_wx_catlog_list":
                    map.put("market_wx_catlog_list",system.getKeyValue());
                    break;
                case "market_wx_index_brand":
                    map.put("market_wx_index_brand",system.getKeyValue());
                    break;
                case "market_wx_index_hot":
                    map.put("market_wx_index_hot",system.getKeyValue());
                    break;
                case "market_wx_index_new":
                    map.put("market_wx_index_new",system.getKeyValue());
                    break;
                case "market_wx_index_topic":
                    map.put("market_wx_index_topic",system.getKeyValue());
                    break;
                case "market_wx_share":
                    map.put("market_wx_share",system.getKeyValue());
                    break;
            }
        }
        sqlSession.close();
        return map;
    }

    @Override
    public void wxModify(Map<String, Object> map) {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketSystemMapper mapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        List<MarketSystem> systems = mapper.selectByExample(example);
        //更新数据
        for (MarketSystem system : systems) {
            String keyName = system.getKeyName();
            switch (keyName){
                case "market_wx_catlog_goods":
                    system.setKeyValue(map.get("market_wx_catlog_goods").toString());
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_wx_catlog_list":
                    system.setKeyValue(map.get("market_wx_catlog_list").toString());
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_wx_index_brand":
                    system.setKeyValue(map.get("market_wx_index_brand").toString());
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_wx_index_hot":
                    system.setKeyValue(map.get("market_wx_index_hot").toString());
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_wx_index_new":
                    system.setKeyValue(map.get("market_wx_index_new").toString());
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_wx_index_topic":
                    system.setKeyValue(map.get("market_wx_index_topic").toString());
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_wx_share":
                    String share = map.get("market_wx_share").toString();
                    system.setKeyValue(share);
                    mapper.updateByPrimaryKey(system);
                    break;
            }

        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Map<String, String> expressQuery() {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketSystemMapper mapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        List<MarketSystem> systems = mapper.selectByExample(example);
        Map<String,String> map = new HashMap<>();
        for (MarketSystem system : systems) {
            String keyName = system.getKeyName();
            switch (keyName){
                case "market_express_freight_min":
                    map.put("market_express_freight_min",system.getKeyValue());
                    break;
                case "market_express_freight_value":
                    map.put("market_express_freight_value",system.getKeyValue());
                    break;
            }
        }
        sqlSession.close();
        return map;
    }

    @Override
    public void expressModify(Map<String, String> map) {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketSystemMapper mapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        List<MarketSystem> systems = mapper.selectByExample(example);
        //更新数据
        for (MarketSystem system : systems) {
            String keyName = system.getKeyName();
            switch (keyName){
                case "market_express_freight_min":
                    system.setKeyValue(map.get("market_express_freight_min"));
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_express_freight_value":
                    system.setKeyValue(map.get("market_express_freight_value"));
                    mapper.updateByPrimaryKey(system);
                    break;
            }

        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Map<String, String> orderQuery() {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketSystemMapper mapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        List<MarketSystem> systems = mapper.selectByExample(example);
        Map<String,String> map = new HashMap<>();
        for (MarketSystem system : systems) {
            String keyName = system.getKeyName();
            switch (keyName){
                case "market_order_comment":
                    map.put("market_order_comment",system.getKeyValue());
                    break;
                case "market_order_unconfirm":
                    map.put("market_order_unconfirm",system.getKeyValue());
                    break;
                case "market_order_unpaid":
                    map.put("market_order_unpaid",system.getKeyValue());
                    break;
            }
        }
        sqlSession.close();
        return map;
    }

    @Override
    public void orderModify(Map<String, String> map) {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketSystemMapper mapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        List<MarketSystem> systems = mapper.selectByExample(example);
        //更新数据
        for (MarketSystem system : systems) {
            String keyName = system.getKeyName();
            switch (keyName){
                case "market_order_comment":
                    system.setKeyValue(map.get("market_order_comment"));
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_order_unconfirm":
                    system.setKeyValue(map.get("market_order_unconfirm"));
                    mapper.updateByPrimaryKey(system);
                    break;
                case "market_order_unpaid":
                    system.setKeyValue(map.get("market_order_unpaid"));
                    mapper.updateByPrimaryKey(system);
                    break;
            }

        }
        sqlSession.commit();
        sqlSession.close();
    }


}
